package com.xb.loan.admin.web.controller.app;

import com.xb.hehe.app.dto.TLoanPlatformDTO;
import com.xb.loan.admin.bean.dict.District;
import com.xb.loan.admin.service.app.PlatformServer;
import com.xb.loan.admin.util.ObjectUtil;
import com.xb.loan.admin.web.controller.BaseController;
import com.xb.loan.log.exception.BizException;
import com.xb.loan.util.DateHelper;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/app")
public class PlatformController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(PlatformController.class);

    @Autowired
    PlatformServer platformServer;

    @RequestMapping(value = "showPlatform", method = RequestMethod.GET)
    public String showPlatform(Model model, HttpServletRequest req) throws BizException {
        return "/app/platform/showPlatform";
    }

    @ResponseBody
    @RequestMapping(value = "queryPlatformDate", method = RequestMethod.POST)
    public Map<String, Object> queryPlatformDate(HttpServletRequest req) {
        logger.info("PlatformController.queryPlatformDate ");

        String isRelease = ObjectUtil.formatString(req.getParameter("isRelease"));
        String platformName = ObjectUtil.formatString(req.getParameter("platformName"));


        Map<String, Object> map = new HashMap<String, Object>();
        int pageNum = ObjectUtil.formatString2Integer(req.getParameter("pageNum")) == null ?
                this.getPageNum() : ObjectUtil.formatString2Integer(req.getParameter("pageNum"));
        int pageSize = ObjectUtil.formatString2Integer(req.getParameter("numPerPage")) == null ?
                this.getNumPerPage() : ObjectUtil.formatString2Integer(req.getParameter("numPerPage"));

        int isReleaseId = -1;
        if(isRelease != null && isRelease.length() > 0){
            isReleaseId = Integer.parseInt(isRelease);
        }

        List<Map<String, Object>> list = platformServer.queryPlatformList(pageSize, pageNum,isReleaseId,platformName);
        int totalCount = platformServer.queryPlatformCount(isReleaseId,platformName);
        map.put("json", list);
        map.put("totalCount", totalCount);
        map.put("pageNum", pageNum);
        map.put("numPerPage", pageSize);
        return map;
    }

    @RequiresPermissions("101010100")
    @RequestMapping(value="showSaveOrUpdatePlatform",method=RequestMethod.GET)
    public String showSaveOrUpdatePlatform(Model model,HttpServletRequest req,
                                          @RequestParam(value="type",required=true)String type,
                                          @RequestParam(value="id",required=false) String id){
        try {
            Long tid = 0L;
            if(id != null && id.length() > 0){
                tid = Long.parseLong(id);
            }

            if ("update".equals(type) && tid > 0) {
                TLoanPlatformDTO tLoanPlatformDTO = platformServer.queryTLoanPlatformDTOById(id);
                model.addAttribute("platformDto", tLoanPlatformDTO);
            }else{
                type ="save";
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        model.addAttribute("type",type);
        return "/app/platfrom/saveOrUpdatePlatform";
    }

    @ResponseBody
    @RequestMapping(value = "validatePlatformId", method = RequestMethod.POST)
    public Map<String, Object> validatePlatformId(String id, HttpServletRequest req) {
        logger.info("PlatformController.validatePlatformId ");
        Map<String, Object> map = new HashMap<String, Object>();

        int errorCode = 0;

        if (id == null || id.length() < 1) {
            errorCode = 2;
        }

        TLoanPlatformDTO tLoanPlatformDTO = platformServer.queryTLoanPlatformDTOById(id);
        if(tLoanPlatformDTO != null && tLoanPlatformDTO.getId() != null && tLoanPlatformDTO.getId().length() > 0){
            errorCode = 1;
        }
        map.put("key", errorCode);
        return map;
    }

    @ResponseBody
    @RequestMapping(value="deletePlatform",method=RequestMethod.POST )
    public Map<String, Object> deletePlatform(String id,String isRelease,HttpServletRequest req){
        logger.info("PlatformController.deletePlatform id="+id);
        String loginOperatorId = this.getOperatorId(req);

        Map<String, Object> map = new HashMap<String, Object>();
        String msg = "操作成功";
        int errorCode = 0;
        if(id != null && id.length() > 0 && isRelease != null && isRelease.length() > 0){
            try {
                TLoanPlatformDTO tLoanPlatformDTO = new TLoanPlatformDTO();
                tLoanPlatformDTO.setId(id);
                tLoanPlatformDTO.setIsRelease(Integer.parseInt(isRelease));
                platformServer.updateTLoanPlatformDTO(tLoanPlatformDTO);
            } catch (Exception e) {
                msg = e.getMessage();
                errorCode = 1;
                logger.error(msg,e);
            }
        }else{
            msg = "编码错误";
            errorCode = 1;
        }
        map.put("msg", msg);
        map.put("errorCode", errorCode);
        return map;
    }
}
